package com.File_io_.homework;

import java.io.*;
import java.util.Properties;

/**
 * @author 宋鑫宇
 * @version 1.0
 */
public class Homework03 {
    public static void main(String[] args) throws Exception {
        //创建对象 Properties
        Properties properties = new Properties();
        //输入k-v值
        properties.setProperty("name", "tom");
        properties.setProperty("age", "5");
        properties.setProperty("color", "red");
        //将k-v保存到路径文件中
        properties.store(new FileWriter("D:\\idea_WorkSpace\\myjava\\myjava\\src\\com\\File_io_\\homework\\mytemp\\dog.properties"), null);
        //创建Dog对象 用getProperty方法返回的字符串给Dog类赋值
        Dog dog = new Dog(properties.getProperty("name"), (Integer.parseInt(properties.getProperty("age"))), properties.getProperty("color"));
        System.out.println(dog);
        //创建序列化对象 ObjectOutputStream
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:\\idea_WorkSpace\\myjava\\myjava\\src\\com\\File_io_\\homework\\mytemp\\dog.dat"));
        //把Dog对象序列化到指定目录文件
        oos.writeObject(dog);
        oos.close();
        m1();
    }

    //反序列化输出Dog对象信息
    public static void m1() throws Exception {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\idea_WorkSpace\\myjava\\myjava\\src\\com\\File_io_\\homework\\mytemp\\dog.dat"));
        Dog dog = (Dog) ois.readObject();
        System.out.println(dog);
        ois.close();
    }
}

class Dog implements Serializable {
    private String name;
    private int age;
    private String color;

    public Dog(String name, int age, String color) {
        this.name = name;
        this.age = age;
        this.color = color;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age='" + age + '\'' +
                ", color='" + color + '\'' +
                '}';
    }
}
